package com.lym.service.impl;

import com.car.entity.*;
import com.car.mapper.*;
import com.car.vo.CarInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lym.service.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName BaseService
 * @Description: TODO
 * @Author ForeverLoveT
 * @Date 2020/3/9
 * @Version V1.0
 **/
@Service
public class BaseServiceImpl implements BaseService {
    @Autowired
    private CarInfoMapper carInfoMapper;
    @Autowired
    private CellPackMapper cellPackMapper;
    @Autowired
    private CarBatteryMapper carBatteryMapper;
    @Override
    /** 
     * @MethodName: findAll 查询车的基本信息
     * @Description: TODO
     * @Param: [name, vin, pageNum, pageSize]
     * @Return: com.github.pagehelper.PageInfo<com.car.vo.CarInfoVo>
     * @Author: ForeverLoveT
     * @Date: 18:24
    **/
    public PageInfo<CarInfoVo> findAll(String name, String vin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);//使用pageHelper插件
        List<CarInfoVo> carList = carInfoMapper.findCarList(name, vin);//查询
        PageInfo<CarInfoVo> pageInfo = new PageInfo<>(carList);//进行分页
        for (CarInfoVo a:carList) {
            List<CellPack> cellPacks = a.getCellPacks();
            for (CellPack c : cellPacks) {
            if (c.getName() == null) {//判断是否配电
                a.setIsPCell("配电");
            } else {
                a.setIsPCell("已配电");
            }
        }
        }
        return pageInfo;
    }

    /**
     * 查询未配电的电池
     * @param state
     * @return
     */
    @Override
    public List<CellPack> getClell(String state) {
        return cellPackMapper.findWCell(state);
    }
/**
 * @MethodName: PeiDian  给没有配电的车进行配电
 * @Description: TODO
 * @Param: [carid, cellid]
 * @Return: void
 * @Author: ForeverLoveT
 * @Date: 18:25
**/
    @Override
    public void PeiDian(String carid, String cellid) {
        CarBattery carBattery = new CarBattery();
        carBattery.setId(UUID.randomUUID().toString());
        carBattery.setCarId(carid);
        carBattery.setCellId(cellid);
        carBatteryMapper.insert(carBattery);
        CellPack pack = cellPackMapper.selectByPrimaryKey(cellid);
        pack.setState("1");//改为状态已配电
        cellPackMapper.updateByPrimaryKey(pack);
    }
    /** 
     * @MethodName: findOneCell  查询一个车配的那个电池
     * @Description: TODO
     * @Param: [carid]
     * @Return: com.car.entity.CellPack
     * @Author: ForeverLoveT
     * @Date: 18:23
    **/
    @Override
    public CellPack findOneCell(String carid) {
        CarBattery carBattery= carBatteryMapper.findOne(carid);
        String cellId = carBattery.getCellId();
        CellPack pack = cellPackMapper.selectByPrimaryKey(cellId);
//        String companyId = pack.getCompanyId();
//        CellCompany cellCompany = cellCompanyMapper.selectByPrimaryKey(companyId);
//        String typeId = pack.getTypeId();
//        CellType cellType = cellTypeMapper.selectByPrimaryKey(typeId);
        return pack;
    }
    /**
     * @MethodName: getCarAll  车的详细信息
     * @Description: TODO
     * @Param: [carid]
     * @Return: com.car.vo.CarInfoVo
     * @Author: ForeverLoveT
     * @Date: 18:23
    **/
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public CarInfoVo getCarAll(String carid) {
        //查询车辆的单条数据
        CarInfo carInfo = carInfoMapper.selectByPrimaryKey(carid);
        //获取基本信息
        Map basicInfo = redisTemplate.opsForHash().entries("BasicInfo");
        BasicInfo basicInfo1 = (BasicInfo) basicInfo.get(carInfo.getCarBasic());
        //获取sizeInfo
        Map sizeInfo = redisTemplate.opsForHash().entries("SizeInfo");
        SizeInfo sizeInfo1 = (SizeInfo) sizeInfo.get(carInfo.getCarSize());
        //获取power
        Map powerSystem = redisTemplate.opsForHash().entries("PowerSystem");
        PowerSystem powerSystem1 = (PowerSystem) powerSystem.get(carInfo.getCarDynamical());
        //获取chass
        Map chassisBrake = redisTemplate.opsForHash().entries("ChassisBrake");
        ChassisBrake chassisBrake1 = (ChassisBrake) chassisBrake.get(carInfo.getCarUnderpan());
        //获取sec
        Map securityConfiguration = redisTemplate.opsForHash().entries("SecurityConfiguration");
        SecurityConfiguration securityConfiguration1 = (SecurityConfiguration) securityConfiguration.get(carInfo.getCarAfety());
        System.out.println(securityConfiguration1);//打印信息
        //获取driv
        Map drivigAssistance = redisTemplate.opsForHash().entries("DrivigAssistance");
        DrivigAssistance drivigAssistance1 = (DrivigAssistance) drivigAssistance.get(carInfo.getCarDriving());
        //获取exter
        Map externalConfig = redisTemplate.opsForHash().entries("ExternalConfig");
        ExternalConfig externalConfig1 = (ExternalConfig) externalConfig.get(carInfo.getCarExternal());
        //获取seat
        Map seatConfiguration = redisTemplate.opsForHash().entries("SeatConfiguration");
        SeatConfiguration seatConfiguration1 = (SeatConfiguration) seatConfiguration.get(carInfo.getCarSeat());
        //获取sinteriorConfig
        Map interiorConfig = redisTemplate.opsForHash().entries("InteriorConfig");
        InteriorConfig interiorConfig1 = (InteriorConfig) interiorConfig.get(carInfo.getCarInterior());
        //获取informationEntertainment
        //Map informationEntertainment = redisTemplate.opsForHash().entries("InformationEntertainment");
       // InformationEntertainment informationEntertainment1 = (InformationEntertainment) informationEntertainment.get(carInfo.getCarYl());
        CarInfoVo carInfoVo = new CarInfoVo();
        carInfoVo.setBasicInfo(basicInfo1);
        carInfoVo.setSizeInfo(sizeInfo1);
        carInfoVo.setPowerSystem(powerSystem1);
        carInfoVo.setChassisBrake(chassisBrake1);
        carInfoVo.setSeatConfiguration(seatConfiguration1);
        carInfoVo.setSecurityConfiguration(securityConfiguration1);
        carInfoVo.setDrivingAssistance(drivigAssistance1);
        carInfoVo.setExternalConfig(externalConfig1);
       // carInfoVo.setInformationEntertainment(informationEntertainment1);
        carInfoVo.setInteriorConfig(interiorConfig1);
        BeanUtils.copyProperties(carInfo,carInfoVo);

        System.err.println(carInfo);
        return carInfoVo;
    }
    @Autowired
    private SizeInfoMapper sizeInfoMapper;
    @Autowired
    private PowerSystemMapper powerSystemMapper;
    @Autowired
    private ChassisBrakeMapper chassisBrakeMapper;
    @Autowired
    private DrivigAssistanceMapper drivigAssistanceMapper;
    @Autowired
    private SecurityConfigurationMapper securityConfigurationMapper;
    @Autowired
    private ExternalConfigMapper externalConfigMapper;
    @Autowired
    private InteriorConfigMapper interiorConfigMapper;
    @Autowired
    private SeatConfigurationMapper seatConfigurationMapper;
    @Autowired
    private InformationEntertainmentMapper informationEntertainmentMapper;

        //汽车添加
    @Override
    public void AddCar(CarInfoVo carInfoVo) {
        CarInfo carInfo = new CarInfo();
        carInfo.setId(UUID.randomUUID().toString());
        carInfo.setCarAfety(carInfoVo.getCarAfety());
        carInfo.setCarBasic(carInfoVo.getCarBasic());
        carInfo.setCarDriving(carInfoVo.getCarDriving());
        carInfo.setCarDynamical(carInfoVo.getCarDynamical());
        carInfo.setCarExternal(carInfoVo.getCarExternal());
        carInfo.setCarInterior(carInfoVo.getCarInterior());
        carInfo.setCarMileage(carInfoVo.getCarMileage());
        carInfo.setCarName(carInfoVo.getCarName());;
        carInfo.setCarSeat(carInfoVo.getCarSeat());
        carInfo.setCarSize(carInfoVo.getCarSize());
        carInfo.setCarStuta(carInfoVo.getCarStuta());
        carInfo.setCarUnderpan(carInfoVo.getCarUnderpan());
        carInfo.setCarVin(carInfoVo.getCarVin());
        carInfo.setCarYl(carInfoVo.getCarYl());
        carInfo.setCreateDate(carInfoVo.getCreateDate());
        carInfo.setTrack(carInfoVo.getTrack());
        carInfo.setUserId(carInfoVo.getUserId());
        carInfo.setUpdateDate(carInfoVo.getUpdateDate());
        carInfoMapper.insert(carInfo);
        sizeInfoMapper.insert(carInfoVo.getSizeInfo());
        powerSystemMapper.insert(carInfoVo.getPowerSystem());
        chassisBrakeMapper.insert(carInfoVo.getChassisBrake());
        drivigAssistanceMapper.insert(carInfoVo.getDrivingAssistance());
        securityConfigurationMapper.insert(carInfoVo.getSecurityConfiguration());
        externalConfigMapper.insert(carInfoVo.getExternalConfig());
        interiorConfigMapper.insert(carInfoVo.getInteriorConfig());
        seatConfigurationMapper.insert(carInfoVo.getSeatConfiguration());
        informationEntertainmentMapper.insert(carInfoVo.getInformationEntertainment());
    }
}
